Socket
Socket
Sign inDemoInstall

@aws-cdk/aws-iam

Package Overview
Dependencies
Maintainers
5
Versions
288
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aws-cdk/aws-iam

CDK routines for easily assigning correct and minimal IAM permissions


Version published
Weekly downloads
0
Maintainers
5
Install size
3.00 MB
Created
Weekly downloads
 

Package description

What is @aws-cdk/aws-iam?

@aws-cdk/aws-iam is an AWS Cloud Development Kit (CDK) library that allows you to define AWS Identity and Access Management (IAM) resources in your CDK applications. This package provides constructs for creating and managing IAM roles, users, policies, and groups, enabling you to manage permissions and access control in your AWS environment programmatically.

What are @aws-cdk/aws-iam's main functionalities?

Create IAM Role

This code sample demonstrates how to create an IAM role that can be assumed by EC2 instances and has read-only access to Amazon S3.

const iam = require('@aws-cdk/aws-iam');
const cdk = require('@aws-cdk/core');

class MyStack extends cdk.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    new iam.Role(this, 'MyRole', {
      assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
      managedPolicies: [
        iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonS3ReadOnlyAccess')
      ]
    });
  }
}

const app = new cdk.App();
new MyStack(app, 'MyStack');

Create IAM User

This code sample demonstrates how to create an IAM user with administrator access.

const iam = require('@aws-cdk/aws-iam');
const cdk = require('@aws-cdk/core');

class MyStack extends cdk.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    new iam.User(this, 'MyUser', {
      userName: 'my-user',
      managedPolicies: [
        iam.ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess')
      ]
    });
  }
}

const app = new cdk.App();
new MyStack(app, 'MyStack');

Attach Inline Policy to Role

This code sample demonstrates how to create an IAM role and attach an inline policy that allows listing objects in a specific S3 bucket.

const iam = require('@aws-cdk/aws-iam');
const cdk = require('@aws-cdk/core');

class MyStack extends cdk.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    const role = new iam.Role(this, 'MyRole', {
      assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com')
    });

    role.addToPolicy(new iam.PolicyStatement({
      actions: ['s3:ListBucket'],
      resources: ['arn:aws:s3:::my-bucket']
    }));
  }
}

const app = new cdk.App();
new MyStack(app, 'MyStack');

Other packages similar to @aws-cdk/aws-iam

Changelog

Source

0.34.0 (2019-06-07)

Bug Fixes

  • build: Correct buildspec so it does not fail (#2737) (e362ac8)
  • certificatemanager: correct certificateArn typo in the README (#2712) (2bfc1c2)
  • cli: don't fail if region cannot be determined (#2721) (0c72ef3), closes #2697
  • cli: remove support for applets (#2691) (0997ee2)
  • cloudwatch: move SNS Alarm Action to aws-cloudwatch-actions (#2688) (e3df21a)
  • codebuild: grant the Project's Role permissions to the KMS Key if it was passed. (#2715) (4e12fe6)
  • core: apply overrides after rendering properties (#2685) (f2636e5), closes #2677
  • core: Make filterUndefined null-safe (#2789) (e4fb811), closes awslabs/jsii#523
  • ecs: remove LoadBalancedFargateServiceApplet, no longer supported (#2779) (a610017)
  • ecs-patterns: expose service on queue worker services (#2780) (6d83cb9)
  • pkglint: Adjust stability banner style (#2768) (da94d8b)
  • route53: support zone roots as record names (#2705) (08a2852)
  • stepfunctions: improve Task payload encoding (#2706) (1c13faa)

Code Refactoring

Features

BREAKING CHANGES TO EXPERIMENTAL FEATURES

  • route53: recordValue: string prop in route53.TxtRecord changed to values: string[]
  • recordValue prop in route53.CnameRecord renamed to domainName
  • route53.AliasRecord has been removed, use route53.ARecord or route53.AaaaRecord with the target prop.
  • kms: The EncryptionKeyAlias class was renamed to Alias. Associated types (such as EncryptionKeyAliasProps) were renamed in the same way.
  • cli: This release requires CDK CLI >= 0.34.0
  • core: App.run() was renamed to App.synth() (soft deprecation, it will be removed in the next release).
  • core: node.stack is now Stack.of(construct) (fixes #2766)
  • core: node.resolve has been moved to stack.resolve.
  • core: node.stringifyJson has been moved to stack.stringifyJson.
  • core: node.validateTree is now ConstructNode.validate(node)
  • core: node.prepareTree is now ConstructNode.prepare(node)
  • core: node.getContext is now node.tryGetContext
  • core: node.recordReference is now node.addReference
  • core: node.apply is now node.applyAspect
  • core: node.ancestors() is now node.scopes
  • core: node.required has been removed.
  • core: node.typename has been removed.
  • core: node.addChild is now private
  • core: node.findReferences() is now node.references
  • core: node.findDependencies() is now node.dependencies
  • core: stack.dependencies() is now stack.dependencies
  • core: CfnElement.stackPath has been removed.
  • core: CloudFormationLang is now internal (use stack.toJsonString())
  • cloudwatch: using an SNS topic as CloudWatch Alarm Actxion now requires an integration object from the @aws-cdk/aws-cloudwatch-actions package.
  • event-targets: targets.EcsEc2Task renamed to targets.EcsTask
  • SNS - Subscription endpoint is now type string (previously any)
  • Step Functions - result in the Pass state is now type map (previously any)
  • the following modules are no longer released: @aws-cdk/applet-js, @aws-cdk/aws-autoscaling-api, @aws-cdk/aws-codedeploy-api
  • cli: applets are no longer supported as an app type, use "decdk" instead.
  • core: Properties passed to addPropertyOverride should match in capitalization to the CloudFormation schema (normally pascal case). For example, addPropertyOverride('accessControl', 'xxx') should now be addPropertyOverride('AccessControl', 'xxx').
  • rds: rds.RotationSingleUser renamed to rds.SecretRotation
  • rds: rds.ClusterParameterGroup no longer has setParameter() and removeParameter() methods, use the parameters prop directly in the constructor instead.

Readme

Source

AWS Identity and Access Management Construct Library


Stability: Experimental

This API is still under active development and subject to non-backward compatible changes or removal in any future version. Use of the API is not recommended in production environments. Experimental APIs are not subject to the Semantic Versioning model.


Define a role and add permissions to it. This will automatically create and attach an IAM policy to the role:

attaching permissions to role

Define a policy and attach it to groups, users and roles. Note that it is possible to attach the policy either by calling xxx.attachInlinePolicy(policy) or policy.attachToXxx(xxx).

attaching policies to user and group

Managed policies can be attached using xxx.attachManagedPolicy(arn):

attaching managed policies

Configuring an ExternalId

If you need to create roles that will be assumed by 3rd parties, it is generally a good idea to require an ExternalId to assume them. Configuring an ExternalId works like this:

supplying an external ID

Principals vs Identities

When we say Principal, we mean an entity you grant permissions to. This entity can be an AWS Service, a Role, or something more abstract such as "all users in this account" or even "all users in this organization". An Identity is an IAM representing a single IAM entity that can have a policy attached, one of Role, User, or Group.

IAM Principals

When defining policy statements as part of an AssumeRole policy or as part of a resource policy, statements would usually refer to a specific IAM principal under Principal.

IAM principals are modeled as classes that derive from the iam.PolicyPrincipal abstract class. Principal objects include principal type (string) and value (array of string), optional set of conditions and the action that this principal requires when it is used in an assume role policy document.

To add a principal to a policy statement you can either use the abstract statement.addPrincipal, one of the concrete addXxxPrincipal methods:

  • addAwsPrincipal, addArnPrincipal or new ArnPrincipal(arn) for { "AWS": arn }
  • addAwsAccountPrincipal or new AccountPrincipal(accountId) for { "AWS": account-arn }
  • addServicePrincipal or new ServicePrincipal(service) for { "Service": service }
  • addAccountRootPrincipal or new AccountRootPrincipal() for { "AWS": { "Ref: "AWS::AccountId" } }
  • addCanonicalUserPrincipal or new CanonicalUserPrincipal(id) for { "CanonicalUser": id }
  • addFederatedPrincipal or new FederatedPrincipal(federated, conditions, assumeAction) for { "Federated": arn } and a set of optional conditions and the assume role action to use.
  • addAnyPrincipal or new AnyPrincipal for { "AWS": "*" }

If multiple principals are added to the policy statement, they will be merged together:

const statement = new PolicyStatement();
statement.addServicePrincipal('cloudwatch.amazonaws.com');
statement.addServicePrincipal('ec2.amazonaws.com');
statement.addAwsPrincipal('arn:aws:boom:boom');

Will result in:

{
  "Principal": {
    "Service": [ "cloudwatch.amazonaws.com", "ec2.amazonaws.com" ],
    "AWS": "arn:aws:boom:boom"
  }
}

The CompositePrincipal class can also be used to define complex principals, for example:

const role = new iam.Role(this, 'MyRole', {
  assumedBy: new iam.CompositePrincipal(
    new iam.ServicePrincipal('ec2.amazonaws.com'),
    new iam.AccountPrincipal('1818188181818187272')
  )
});

Features

  • Policy name uniqueness is enforced. If two policies by the same name are attached to the same principal, the attachment will fail.
  • Policy names are not required - the CDK logical ID will be used and ensured to be unique.

Keywords

FAQs

Package last updated on 10 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc